package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
@Slf4j
public class CommonExceptionResolver {
    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e) {
        e.printStackTrace();
        String message = e.getMessage();
        if (message.contains("Duplicate entry")) {
            String[] split = message.split("'");
            return R.error("该用户名" + split[1] + "已存在");
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(BusinessException.class)
    public R<String> exceptionHandler(BusinessException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
}
